package dao;

import entity.WarningRecord;
import util.DBUtil;

import java.sql.*;
import java.util.*;

public class WarningRecordDAO {

    // 获取所有预警记录
    public static List<WarningRecord> getAllWarningRecords() {
        List<WarningRecord> warningRecords = new ArrayList<>();
        String query = "SELECT * FROM warning_records ORDER BY created_at DESC";

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement stmt = conn.prepareStatement(query);
             ResultSet rs = stmt.executeQuery()) {

            while (rs.next()) {
                WarningRecord record = new WarningRecord(
                        rs.getInt("id"),
                        rs.getString("warning_type"),
                        rs.getString("description"),
                        rs.getTimestamp("created_at"), // 确保使用的是java.sql.Timestamp
                        rs.getInt("status")
                );
                warningRecords.add(record);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        return warningRecords;
    }
}
